﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightGame
{
    public class Player : AnimatedGameObject
    {
        public const double TIME_BETWEEN_SHOTS = 0.25;
        public const double SPEED = 200;
        public const string PLAYER = "player";
        public const int PLAYER_Z = 10;
        protected double timeSinceLastShot = 0;

        /// <summary>
        /// Property to notify the application manager of the lives count
        /// </summary>
        protected int lives = 5;
        public int Lives
        {
            get
            {
                return lives;
            }
            set
            {
                lives = value;
                ApplicationManager.Instance.Lives = value;
            }
        }

        public Player()
        {

        }

        public void startupPlayer(Point dimensions, AnimationData animationData, int zLayer)
        {
            base.startupAnimatedGameObject(dimensions, animationData, zLayer, false);
            this.collisionName = PLAYER;
            ApplicationManager.Instance.Lives = lives;
        }

        public override void enterFrame(double dt)
        {
            base.enterFrame(dt);

            timeSinceLastShot -= dt;
            if (KeyHandler.Instance.isKeyPressed(Key.Space) && timeSinceLastShot <= 0)
            {
                timeSinceLastShot = TIME_BETWEEN_SHOTS;
                Weapon weapon = Weapon.UnusedWeapon.startupPlayerBasicWeapon(PLAYER_Z);
                weapon.Position = new Point(Position.X + dimensions.X / 2 - weapon.Dimensions.X / 2, Position.Y - weapon.Dimensions.Y);
            }

            if (KeyHandler.Instance.isKeyPressed(Key.Up))
            {
                Position = new Point(Position.X, Position.Y - SPEED * dt);
            }
            else if (KeyHandler.Instance.isKeyPressed(Key.Down))
            {
                Position = new Point(Position.X, Position.Y +SPEED * dt);
            }

            if (KeyHandler.Instance.isKeyPressed(Key.Left))
            {
                Position = new Point(Position.X - SPEED * dt, Position.Y);
            }
            else if (KeyHandler.Instance.isKeyPressed(Key.Right))
            {
                Position = new Point(Position.X + SPEED * dt, Position.Y);
            }

            // keep the player bound to the screen
            if (Position.X > (SilverlightGame.App.Current.RootVisual as Page).Width - dimensions.X)
                Position = new Point((SilverlightGame.App.Current.RootVisual as Page).Width - dimensions.X, Position.Y);
            else if (Position.X < 0)
                Position = new Point(0, Position.Y);
            if (Position.Y > (SilverlightGame.App.Current.RootVisual as Page).Height - dimensions.Y)
                Position = new Point(Position.X, (SilverlightGame.App.Current.RootVisual as Page).Height - dimensions.Y);
            else if (Position.Y < 0)
                Position = new Point(Position.X, 0);
        }

        public override void shutdown()
        {
            base.shutdown();
        }

        public override void collision(GameObject other)
        {
            base.collision(other);

            if (other is Enemy)
            {
                AnimatedGameObject.UnusedAnimatedGameObject.startupAnimatedGameObject(
                    new Point(55, 55),
                    new AnimationData(
                        new string[] { 
                            "Media/Explosion1.png", 
                            "Media/Explosion2.png", 
                            "Media/Explosion3.png", 
                            "Media/Explosion4.png", 
                            "Media/Explosion5.png", 
                            "Media/Explosion6.png", 
                            "Media/Explosion7.png" },
                        20),
                        PLAYER_Z,
                    true).Position = new Point(
                        Position.X + Dimensions.X / 2 - 55 / 2,
                        Position.Y + Dimensions.Y / 2 - 55 / 2
                    );

                Lives -= 1;
                if (Lives <= 0)
                {
                    this.shutdown();
                    ApplicationManager.Instance.resetGame();
                }
            }
            else if (other is Powerup)
            {
                Lives += 1;
            }
        }
    }
}
